** Figure A4 - Year-by-year impact of insurance coverage, controlling for baseline
* Sample used to study household outcomes


clear
clear mata
clear matrix
set maxvar 20000
set more off

pwd
cap cd c(`pwd')

estimates clear

use "../dta/reduced-form-master.dta", clear

* keeping balanced panel
keep if ex_post_balp==1

sort id mkt_year

* Easier labelling for graphs
la var w_real_sav "savings (INR)"
la var w_real_lent "money lent out (INR)"
la var w_real_borrow "money borrowed (INR)"
la var w_real_cons "total consumption (INR)"
la var w_real_food_exp "food expenditure (INR)"
la var w_real_nonfood_exp "non-food expenditure (INR)"
la var w_real_durables_exp "durables expenditure (INR)"
la var w_real_events_exp "events expenditure (INR)"
la var w_real_income "annual income(INR)"
la var w_real_gifts_out "gifts/transfers made (INR)"
la var w_real_gifts_in "gifts/transfers received (INR)"
la var food_sufficiency_child "food sufficiency for children"
la var fladder_std "financial situation (sd units)"
la var outlook_std "good things happen (sd units)"
la var control_std "control over life (sd units)"
sort id mkt_year

local outcomevars w_real_cons w_real_sav w_real_lent w_real_borrow w_real_food_exp w_real_nonfood_exp w_real_durables_exp w_real_events_exp w_real_income w_real_gifts_out w_real_gifts_in food_sufficiency_child fladder_std outlook_std control_std 

sort id survey_year
by id: gen first_year=survey_year if [_n]==1 

foreach var of local outcomevars {
gen `var'_t0_temp = `var' if survey_year==first_year
bys id: egen `var'_t0 =min(`var'_t0_temp)   
drop `var'_t0_temp
}

foreach var of local outcomevars {
replace `var' = 0 if `var' == .
}


local uniqinst "discount_2007 groupT muslimT hinduT video ppayT vframeT pframeT assigned_risk_ws_2008 discount_2008 rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv bdmperc_2009 fourbdmperc_2009 disc4game_2009 bdmperc_2010 fourbdmperc_2010 disc4game_2010 assigned_risk_ws_2010 assigned_video_test assigned_drought_flyer assigned_subsidies_flyer assigned_loan bdmperc_2011 fourbdmperc_2011 disc4game_2011 bdmperc_2012 fourbdmperc_2012 disc4game_2012 bdmperc_2013 fourbdmperc_2013 disc4game_2013"  /* sewaT peerT assigned_risk_ws_2011 assigned_risk_ws_2012 assigned_risk_ws_2013 mrkt_allnegative mrkt_poslang mrkt_posimg*/

foreach var of varlist `uniqinst' {
	replace `var' = 0 if `var'==.
	replace `var' = 0 if wave==0
	}

foreach var of varlist `uniqinst' {
	replace `var' = 0 if treat_year==0
}


// Year-by-year effects IV - Ind 
foreach ovar of varlist w_real_cons w_real_sav w_real_lent w_real_borrow w_real_food_exp w_real_nonfood_exp w_real_durables_exp w_real_events_exp w_real_income w_real_gifts_out w_real_gifts_in food_sufficiency_child fladder_std outlook_std control_std {
gen `ovar'effect = .
gen `ovar'se = .
gen `ovar'se2 = .
gen `ovar'se_2 = .
forvalues i = 2006/2013 {
ivreg `ovar' (s_policy_units=treat_year `uniqinst') firstt_2007 firstt_2009 lost_w3 resurveyed `ovar'_t0 if mkt_year == `i' , r cluster(villageno)
replace `ovar'effect = _b[s_policy_units] if mkt_year == `i'
replace `ovar'se = _se[s_policy_units] if mkt_year == `i'
lab var `ovar'effect "No. of Policies Purhchased - Effect"
lab var `ovar'se "Standard Errors"
replace `ovar'se2  = `ovar'effect+2*`ovar'se if mkt_year == `i'
replace `ovar'se_2 = `ovar'effect-2*`ovar'se if mkt_year == `i'
}

}

* Not collected in these years
replace w_real_saveffect=. if mkt_year==2007
replace w_real_savse2=. if mkt_year==2007
replace w_real_savse_2=. if mkt_year==2007
replace w_real_lenteffect=. if mkt_year==2008|mkt_year==2007
replace w_real_lentse2=. if mkt_year==2007|mkt_year==2008
replace w_real_lentse_2=. if mkt_year==2007|mkt_year==2008
replace w_real_borroweffect=. if mkt_year==2008|mkt_year==2007
replace w_real_borrowse2=. if mkt_year==2007|mkt_year==2008
replace w_real_borrowse_2=. if mkt_year==2007|mkt_year==2008
replace w_real_gifts_outeffect=. if mkt_year==2008|mkt_year==2007
replace w_real_gifts_outse2=. if mkt_year==2008|mkt_year==2007
replace w_real_gifts_outse_2=. if mkt_year==2008|mkt_year==2007
replace w_real_gifts_ineffect=. if mkt_year==2008|mkt_year==2007
replace w_real_gifts_inse2=. if mkt_year==2008|mkt_year==2007
replace w_real_gifts_inse_2=. if mkt_year==2008|mkt_year==2007
replace outlook_stdeffect=. if mkt_year==2007|mkt_year==2008|mkt_year==2009
replace outlook_stdse2=. if mkt_year==2007|mkt_year==2008|mkt_year==2009
replace outlook_stdse_2=. if mkt_year==2007|mkt_year==2008|mkt_year==2009
replace control_stdeffect=. if mkt_year==2007|mkt_year==2008|mkt_year==2009
replace control_stdse2=. if mkt_year==2007|mkt_year==2008|mkt_year==2009
replace control_stdse_2=. if mkt_year==2007|mkt_year==2008|mkt_year==2009



foreach ovar of varlist w_real_cons w_real_sav w_real_lent w_real_borrow w_real_cons w_real_food_exp w_real_nonfood_exp w_real_durables_exp w_real_events_exp w_real_income w_real_gifts_out w_real_gifts_in food_sufficiency_child fladder_std outlook_std control_std {
local l`ovar': variable label `ovar'
twoway (rarea `ovar'se2 `ovar'se_2 mkt_year, sort astyle(ci ci))(scatter `ovar'effect mkt_year , sort msymbol(O) c(l)), title("`l`ovar''", size(s)) ylabel(,angle (30)) xlabel(2005(2)2013) xtitle("Year") graphregion(color(white)) plotregion(icolor(white)) legend(order (2 1) label(2 "Estimated coefficient") label(1 "95% confidence interval"))
graph save iv`ovar', replace 
}

grc1leg ivw_real_sav.gph ivw_real_lent.gph ivw_real_borrow.gph ivw_real_cons.gph ivw_real_food_exp.gph ivw_real_nonfood_exp.gph ivw_real_durables_exp.gph ivw_real_events_exp.gph ivw_real_gifts_out.gph ivw_real_income.gph ivw_real_gifts_in.gph ivfood_sufficiency_child.gph ivfladder_std.gph ivoutlook_std.gph ivcontrol_std.gph, graphregion(color(white) lwidth(large)) plotregion(icolor(white)) 
graph export "../output/graphs/af4.emf", replace




